<?php

namespace app\controllers;

use app\auth\PlatformAuth;
use app\logics\Common;
use app\logics\User;

class IndexController extends ControllerBase
{
    /**
     * 用户注册接口
     * @param string post mobile
     * @param string post password
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     * @return ['code': ERROR_NONE|ERROR_MOBILE_NUMBER_INVALID|ERROR_PASSWORD_LENGTH_INVALID|ERROR_MOBILE_NUMBER_EXISTS|ERROR_MOBILE_CAPTCHA_INVALID]
     */
    public function registerAction()
    {
        $request = $this->request;
        $user = User::register($request);
        if (!$user) {
            return $this->responseJson(User::$errorCode, []);
        }
        return $this->responseJson(ERROR_NONE, [
            'mobile' => $user->mobile,
            'nickname' => $user->nickname
        ]);
    }

    /**
     * 直接登录用户中心的接口
     */
    public function loginAction()
    {
        $request = $this->request;
        $user = User::login($request);
        if ($user) {
            return $this->responseJson(ERROR_NONE, [
                'uid' => $user->open_id,
                'nickname' => $user->nickname,
                'icon' => $user->icon,
                'token' => $user->last_login_token
            ]);
        }
        return $this->responseJson(User::$errorCode,[]);
    }

    /**
     * app 登录的接口 需要授权
     * @param mobile post
     * @param password post
     * @param app_id post
     * @param platform_id post
     * @param auth_token post
     * @param auth_time post
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     * @errorCode
     * ERROR_NONE|ERROR_PLATFORM_ID_PARAM_EMPTY|ERROR_AUTH_TOKEN_PARAM_EMPTY|ERROR_AUTH_TIME_PARAM_EMPTY|
     * ERROR_APP_ID_PARAM_EMPTY|ERROR_AUTH_TIME_OUT|ERROR_PLATFORM_NOT_EXISTS|ERROR_PLATFORM_ACCESS_AUTH_FAILED|
     * ERROR_LOGIN_MOBILE_OR_PASSWORD_EMPTY|ERROR_MOBILE_NUMBER_NOT_EXISTS|ERROR_LOGIN_PASSWORD_ERROR
     */
    public function appLoginAction()
    {
        $auth = new PlatformAuth();
        if (!$auth->auth()) {
            $this->responseJson($auth::$errorCode, []);
        }
        $mobile = $this->request->getPost('mobile');
        $password = $this->request->getPost('password');
        $appId = $this->request->getPost('app_id');
        $user = User::appLogin($mobile, $password, $appId);
        if (!$user) {
            return $this->responseJson(User::$errorCode, []);
        }
        return $this->responseJson(ERROR_NONE, [
            'uid' => $user->open_id,
            'gender' => $user->gender,
            'age' => $user->age,
            'nickname' => $user->nickname,
            'icon' => $user->getIcon(),
            'app_id' => $appId]);
    }

    /**
     * 发送密码的操作
     * @param post string mobile 手机号码 必须
     * @param post int type 操作类型 非必须 默认为0 0表示注册 1表示找回密码
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     * @errorCode ERROR_NONE | ERROR_MOBILE_NUMBER_INVALID | ERROR_MOBILE_NUMBER_EXISTS | ERROR_OPERATION_FAILED
     * | ERROR_MOBILE_NUMBER_NOT_EXISTS
     */
    public function sendSmsAction()
    {
        //做ip限制 考虑一个ip重复的问题，每个ip每分钟限制请求100次
        $mobile = $this->request->getPost('mobile');
        $type = (int)$this->request->getPost('type');
        if (empty($mobile) || !Common::verifyKenyaMobile($mobile)) {
            return $this->responseJson(ERROR_MOBILE_NUMBER_INVALID, []);
        }
        $user = \app\models\User::findFirst(['conditions' => 'mobile=?0', 'limit' => 1, 'bind' => [$mobile]]);
        switch ($type) {
            //注册 ，检测手机号是否存在
            case 0:
                if ($user) {
                    return $this->responseJson(ERROR_MOBILE_NUMBER_EXISTS, []);
                }
                break;
            //找回密码
            case 1:
                if (!$user) {
                    return $this->responseJson(ERROR_MOBILE_NUMBER_NOT_EXISTS, []);
                }
                break;
            default:
                return $this->responseJson(ERROR_OPERATION_FAILED, ['message' => 'unsupport operate type ' . $type]);
        }
        $ip = $this->request->getClientAddress();
        if (!Common::sendCaptchaToMobile($mobile, $ip, $type)) {
            return $this->responseJson(Common::$errorCode, []);
        }
        return $this->responseJson(ERROR_NONE, []);
    }

    public function testAction()
    {
        $file = $this->request->getUploadedFiles();
        if (count($file) != 1) {
            return $this->responseJson(ERROR_UPLOAD_FILE_NUMBER_ERROR, []);
        }
        $file = $file[0];
        var_dump($file->getRealType(),$file->getKey(),$file->getError(),$file->getExtension(),$file->getType(),$file->getSize(),$file->getName());
    }

}

